package com.zzh.leetCode.LeetCode_20211209;

/**
 * @author：zhangzhanheng
 * @createTime: 2021/12/9  7:13 下午
 * @desc:
 * @version: 1.0.0
 **/
public class Solution {

    public static String longestPalindrome(String s) {
        return  getAllPalindrome(s,0,0,0,0);
    }

    private static String getAllPalindrome(String s,int left,int left1,int right1,int max) {
        for (int i = 1; i < s.length()+1-left; i++) {
            int min = Math.min(left + i, s.length());
            String substring = s.substring(left, min);
            if (isPalindromeOneMethod(substring)) {
                if ((min - left) > max) {
                    left1 = left;
                    right1 = min;
                    max = right1 - left1;
                }
            }
        }
        if (left == s.length()) {
            return  s.substring(left1, right1);
        } else {
            return getAllPalindrome(s,left+1,left1,right1,max);
        }
    }

    public static boolean isPalindromeOneMethod(String x) {
        char[] ch = x.toCharArray();
        if (ch.length == 1) {
            return true;
        } else if (ch.length >= 2){
            for (int i = 0; i < ch.length/2; i++) {
                if (ch[i] != ch[ch.length - i - 1]) {
                    return false;
                }
            }
        }
        return true;
    }

    public static void main(String[] args) {
        long l = System.currentTimeMillis();
        System.out.println(longestPalindrome("abacabbacbadeaa"));
        System.out.println( System.currentTimeMillis() - l );
    }
}
